package com.demo.juc.threadLocal;

/**
 * @Author: yaoshubin
 * @Date: 2023-07-20  19:40
 * @Description: ThreadLocal能够做到在创建变量后，每个线程对变量访问时访问的是线程自己的本地变量
 *
 *
 *
 */
public class ThreadLocalDemo {

    private static ThreadLocal<String> threadLocal = new ThreadLocal<>();

    public static void main(String[] args) {
      /*  new Thread(() -> {
            threadLocal.set(Thread.currentThread().getName());
            System.out.println(Thread.currentThread().getName() + ":" + threadLocal.get());
        }).start();

        new Thread(() -> {
            threadLocal.set(Thread.currentThread().getName());
            System.out.println(Thread.currentThread().getName() + ":" + threadLocal.get());
        }).start();*/


        new Thread(() -> {
            threadLocal.set(Thread.currentThread().getName());
            System.out.println(Thread.currentThread().getName() + " 删除本地变量前:" + threadLocal.get());
            threadLocal.remove();
            System.out.println(Thread.currentThread().getName() + " 删除本地变量后:"  + threadLocal.get());
        }).start();

        new Thread(() -> {
            threadLocal.set(Thread.currentThread().getName());
            System.out.println(Thread.currentThread().getName() + ":" + threadLocal.get());
        }).start();
    }
}
